package cz.myslivec.knihovna.entity;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name="TITLE")
//@SequenceGenerator(name="TITLE_SEQ", sequenceName="TITLE_SEQ", allocationSize = 1)
public class Title implements Serializable {
	private static final long serialVersionUID = -6820814847568472641L;

	@Id
	@Column(name = "TIT_ID")
	@GeneratedValue(strategy=GenerationType.AUTO)
	private long id;

	private String isbn;
	
	private String title;
	
	private String subtitle;
	
	@Column(name = "YEAR_PUBLICATION")
	private Integer yearPublication;
	
	@ManyToOne
	@JoinColumn(name="CATEGORY_CODE")
	private Category category;
	
	private String location;
	
	private Integer bookSerialNmb;
	
	private String note;

	@ManyToMany
	@JoinTable(name="TITLE_AUTHOR", 
			joinColumns = {@JoinColumn(name="TIT_ID", referencedColumnName = "TIT_ID")},
			inverseJoinColumns = {@JoinColumn(name="AUTH_ID", referencedColumnName = "AUTH_ID")})
	private List<Author> authors;

	@ManyToMany
	@JoinTable(name="TITLE_TRANSLATOR", 
			joinColumns = {@JoinColumn(name="TIT_ID", referencedColumnName = "TIT_ID")},
			inverseJoinColumns = {@JoinColumn(name="AUTH_ID", referencedColumnName = "AUTH_ID")})
	private List<Author> translators;

	@ManyToOne
	@JoinColumn(name="PUB_ID")
	private Publisher publisher;

	@OneToMany(mappedBy="title")
	private List<BookCopy> copies;

	
	// GET & SET
	public List<Author> getAuthors() {
		return authors;
	}

	public void setAuthors(List<Author> authors) {
		this.authors = authors;
	}

	public List<Author> getTranslators() {
		return translators;
	}

	public void setTranslators(List<Author> translators) {
		this.translators = translators;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public long getId() {
		return id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getSubtitle() {
		return subtitle;
	}

	public void setSubtitle(String subtitle) {
		this.subtitle = subtitle;
	}

	public String getIsbn() {
		return isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	public Integer getYearPublication() {
		return yearPublication;
	}

	public void setYearPublication(Integer yearPublication) {
		this.yearPublication = yearPublication;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public Integer getBookSerialNmb() {
		return bookSerialNmb;
	}

	public void setBookSerialNmb(Integer bookSerialNmb) {
		this.bookSerialNmb = bookSerialNmb;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public Publisher getPublisher() {
		return publisher;
	}

	public void setPublisher(Publisher publisher) {
		this.publisher = publisher;
	}

	public List<BookCopy> getCopies() {
		return copies;
	}

	public void setCopies(List<BookCopy> copies) {
		this.copies = copies;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Title other = (Title) obj;
		if (id != other.id)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Title [id=" + id + ", isbn=" + isbn + ", title=" + title
				+ ", subtitle=" + subtitle + ", yearPublication="
				+ yearPublication + ", category=" + category + ", location="
				+ location + ", bookSerialNmb=" + bookSerialNmb + ", note="
				+ note + ", authors=" + authors + ", translators="
				+ translators + ", publisher=" + publisher + ", copies="
				+ copies + "]";
	}
}
